package edu.sdbi.judge.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import edu.sdbi.judge.domain.entity.User;
import edu.sdbi.judge.domain.vo.StudentResult;
import edu.sdbi.judge.mapper.CommentMapper;
import edu.sdbi.judge.mapper.UserMapper;
import edu.sdbi.judge.service.GetJudgeIpResultService;
import edu.sdbi.judge.util.HttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.text.SimpleDateFormat;

/**
 * 禁言
 *
 * @author 孙阔
 */
@Order(10)
@Service
public class Judge10 implements GetJudgeIpResultService {

    @Autowired
    private HttpUtil httpUtil;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public StudentResult getJudgeIpResult(String ip) {
        StudentResult studentResult = new StudentResult();
        JSONObject jsonResponse = null;
        double score = 0;
        String banUrl = "http://" + ip + "/comment/ban";
        MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
        String endTime = "2025-12-31 23:59:59";
        String id = "3";
        params.add("userId", id);
        params.add("endTime", endTime);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            String response = httpUtil.doPostFormData(banUrl, null, params);
            jsonResponse = JSON.parseObject(response);
            if (jsonResponse.getInteger("code") == 200) {
                User user = userMapper.getById(id);
                if (user.getEndTime() != null) {
                    if (sdf.format(user.getEndTime()).equals(endTime) && user.getId().equals(id)) {
                        score += 4;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (jsonResponse != null) {
            studentResult.setResult(jsonResponse.toString());
        }
        studentResult.setScore(score);
        studentResult.setItemNumber("10");
        return studentResult;
    }
}
